package my.xh.validate.handler.impl;

import my.xh.validate.ValidateField;
import my.xh.validate.handler.ValidateHandler;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.Map;

/**
 * @ClassName StrValsValidateHandler
 * @Date 2019/1/17 15:25
 * @Author xuanhu
 * @Description string列举值校验
 */
@Component
public class StrValsValidateHandler implements ValidateHandler {
    @Override
    public boolean validate(ValidateField validateField, StringBuilder paramName, Object paramValue, Map<String, Object> map, boolean flag) {
        if (validateField.strVals().length > 0) { // 判断列举的字串值
            try {
                boolean iscontain = false;
                for (String string : validateField.strVals()) {
                    if (paramValue.equals(string)) {
                        iscontain = true;
                        break;
                    }
                }
                if (!iscontain) {
                    flag = false;
                    map.put(paramName.toString(), getMsg(validateField, paramName.append(" 的值不在：").append(Arrays.toString(validateField.strVals())).append(" 之内").toString()));
                }
            } catch (Exception e) {
                flag = false;
                map.put(paramName.toString(), getMsg(validateField, paramName.append(" 的值不在：").append(Arrays.toString(validateField.strVals())).append(" 之内").toString()));
            }
        }
        return flag;
    }
}
